package com.springboot.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.springboot.demo.common.customValidator.ListNotHasNull;
import com.springboot.demo.entity.*;
import com.springboot.demo.entity.data.MsgData;
import com.springboot.demo.entity.validate.scene.*;
import com.springboot.demo.entity.validate.user.IdGroup;
import com.springboot.demo.service.SceneService;
import com.springboot.demo.service.UserService;
import com.springboot.demo.utils.Constant;
import com.springboot.demo.utils.ResponseResult;
import com.springboot.demo.utils.ResponseUtils;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Delete;
import org.aspectj.weaver.ast.Var;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.DefaultBindingErrorProcessor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import javax.validation.groups.Default;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wubc683
 * @desc
 * @date 2021-09-09 9:26
 */
@RestController
@Api(tags = "组态接口")
@Validated
public class SceneController {
    @Autowired
    ResponseUtils responseUtils;
    @Autowired
    SceneService sceneService;

    @GetMapping("/scene/groups")
    @ApiOperation(value = "获取所有组态分组")
    @ApiOperationSupport(order = 1)
    public ResponseUtils<List<SceneGroup>> getSceneGroups(@Valid PageRequest pageRequest) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult sceneGroups = sceneService.getSceneGroups(implUser.getId(), pageRequest);
        return responseUtils.buildSuccessResponse(sceneGroups);
    }

    @PostMapping("/scene/group/{pid}/name/{name}")
    @ApiOperation(value = "创建组态分组", notes = "pid为父目录pid，根目录父id为0")
    @ApiOperationSupport(order = 6)
    public ResponseUtils createSceneGroup(@PathVariable("pid") @ApiParam(value = "父目录id", required = true)
                                          @Min(value = 0, message = "父id必须大于等于0") Long pid,
                                          @PathVariable("name") @ApiParam(value = "组态目录名称，长度6~32,格式：中文a-zA-Z0-9_-",
                                                  required = true)
                                          @Pattern(regexp = "^[\\u4E00-\\u9FA5a-zA-Z0-9_-]{6,32}$", message = "组态文件夹格式错误")
                                          @Length(min = 6, max = 32) String name) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.saveSceneGroup(implUser.getId(), pid, name.trim());
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/scene/group/{pid}/{gid}/name/{name}")
    @ApiOperation(value = "修改组态分组", notes = "pid为父目录pid，根目录父id为0")
    @ApiOperationSupport(order = 7)
    public ResponseUtils updateSceneGroup(@PathVariable("pid") @ApiParam(value = "父文件夹id", required = true)
                                          @Min(value = 0, message = "父id必须大于等于0") Long pid,
                                          @PathVariable("gid") @ApiParam(value = "文件夹id", required = true)
                                          @Min(value = 1, message = "组态分组id必须大于0") Long gid,
                                          @PathVariable("name") @ApiParam(value = "组态目录名称，长度6~32,格式：中文a-zA-Z0-9_-")
                                          @Pattern(regexp = "^[\\u4E00-\\u9FA5a-zA-Z0-9_-]{6,32}$", message = "组态文件夹格式错误")
                                          @Length(min = 6, max = 32) String name) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.updateSceneGroup(implUser.getId(), pid, gid, name.trim());
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/scene/group/{gid}")
    @ApiOperation(value = "删除组态分组")
    @ApiOperationSupport(order = 8)
    public ResponseUtils delSceneGroup(@PathVariable("gid") @ApiParam(value = "文件夹id", required = true)
                                       @Min(value = 1, message = "组态分组id必须大于0") Long gid) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.delSceneGroup(implUser.getId(), gid);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/scenes/info/group")
    @ApiOperation(value = "获取不同分组组态信息", notes = "gid: 0表示未分组，大于0表示组态文件夹id")
    @ApiOperationSupport(order = 9)
    public ResponseUtils<PageResult<Scene>> getDiffGroupSceneInfo(
            @RequestParam(value = "gid", required = false) @Min(value = 0, message = "组态组id最小为0")
            @ApiParam(value = "组态文件夹id") Long gid,
            @Valid PageRequest pageRequest,
            @ApiParam(value = "排序字段(最多1个）,字段名+空格+排序方向,示例：升序[id asc]，降序[id desc],排序字段：name，createDate")
            @RequestParam(required = false) String sort,
            @RequestParam(value = "startDate", required = false) @ApiParam("起始日期")
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
            @RequestParam(value = "endDate", required = false) @ApiParam("结束日期")
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
            @ApiParam(value = "搜索(组态名),6~32字符,格式：中文a-zA-Z0-9_-") @RequestParam(required = false)
            @Pattern(regexp = "^[\\u4E00-\\u9FA5a-zA-Z0-9_-]{6,32}$", message = "组态文件夹格式错误") String search) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult allSceneInfo = sceneService.getDiffGroupSceneInfo(pageRequest, implUser.getId(), gid, sort,
                startDate, endDate, search);
        return responseUtils.buildSuccessResponse(allSceneInfo);
    }

    @GetMapping("/scene/{id}")
    @ApiOperation(value = "获取组态信息")
    @ApiOperationSupport(order = 10)
    public ResponseUtils<Scene> getSceneInfo(@PathVariable("id") Long id) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        Scene sceneInfo = sceneService.getSceneInfo(implUser.getId(), id);
        return responseUtils.buildSuccessResponse(sceneInfo);
    }

    @PostMapping("/scene")
    @ApiOperation(value = "创建组态信息")
    @ApiOperationSupport(order = 20, includeParameters = {"scene.sceneGroupId", "scene.name",
            "scene" + ".description"})
    public ResponseUtils saveSceneInfo(@Validated({SceneGroupIdGroup.class, SceneNameGroup.class,
            DescriptionGroup.class}) @RequestBody Scene scene) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.saveSceneInfo(implUser.getId(), scene);
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/scene/copy/{sceneId}")
    @ApiOperation(value = "复制组态信息")
    @ApiOperationSupport(order = 30)
    public ResponseUtils saveSceneInfo(@PathVariable Long sceneId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.copySceneInfo(implUser.getId(), sceneId);
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/scene/share/{sid}/user/{name}")
    @ApiOperation(value = "分享组态", notes = "把组态分享给xxx用户,被分享用户收集箱会收到申请分享的请求")
    @ApiOperationSupport(order = 40)
    public ResponseUtils shareSceneInfo(@PathVariable("sid") @ApiParam(value = "组态id", required = true) Long sid,
                                        @PathVariable("name") @ApiParam(value = "用户名", required = true) String username) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.shareSceneInfo(implUser.getId(), sid, username.trim());
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/scene/share/reply")
    @ApiOperation(value = "答复分享组态请求")
    @ApiOperationSupport(order = 41)
    public ResponseUtils replyShareSceneInfo(@RequestBody MsgData data) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.replyShareSceneInfo(implUser.getId(), data);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/scene")
    @ApiOperation(value = "修改组态信息")
    @ApiOperationSupport(order = 50, includeParameters = {"scene.id", "scene.sceneGroupId", "scene.name", "scene" +
            ".description", "scene.address"})
    public ResponseUtils updateSceneInfo(@Validated({IdGroup.class, SceneGroupIdGroup.class, SceneNameGroup.class,
            DescriptionGroup.class, SceneAddrGroup.class}) @RequestBody Scene scene) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.updateSceneInfo(implUser.getId(), scene);
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/scenes")
    @ApiOperation(value = "删除一些组态")
    @ApiOperationSupport(order = 60)
    public ResponseUtils delScenes(@RequestBody @ListNotHasNull @Size(min = 1, message = "列表元素最少为1") List<Long> sceneIds) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.delSceneInfo(implUser.getId(), sceneIds);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/scene/{sid}/conn")
    @ApiOperation(value = "获取组态连接相关数据")
    @ApiOperationSupport(order = 70)
    public ResponseUtils<PageResult<Connection>> getSceneConnData(
            @ApiParam(value = "组态id", required = true) @PathVariable("sid") Long sid,
            @Valid PageRequest request) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult sceneConnData = sceneService.getSceneConnData(request, implUser.getId(), sid);
        return responseUtils.buildSuccessResponse(sceneConnData);
    }

    @GetMapping("/scene/conn/{connectId}")
    @ApiOperation(value = "获取组态连接一条数据")
    @ApiOperationSupport(order = 71)
    public ResponseUtils<Connection> getOneSceneConnData(
            @ApiParam(value = "连接id", required = true) @PathVariable("connectId") Long connectId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        Connection oneSceneConnData = sceneService.getOneSceneConnData(implUser.getId(), connectId);
        return responseUtils.buildSuccessResponse(oneSceneConnData);
    }

    @PostMapping("/scene/conn")
    @ApiOperation(value = "创建组态连接")
    @ApiOperationSupport(order = 72, ignoreParameters = {"connection.id"})
    public ResponseUtils saveSceneConnData(
            @Valid @RequestBody Connection connection) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.saveSceneConnData(implUser.getId(), connection);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/scene/conn")
    @ApiOperation(value = "修改组态连接")
    @ApiOperationSupport(order = 74, ignoreParameters = "connection.sceneId")
    public ResponseUtils updateSceneConnData(
            @Validated({IdGroup.class, Default.class}) @RequestBody Connection connection) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.updateSceneConnData(implUser.getId(), connection);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/scene/conn")
    @ApiOperation(value = "删除组态连接")
    @ApiOperationSupport(order = 76)
    public ResponseUtils deleteSceneConnData(
            @RequestBody @ListNotHasNull @Size(min = 1) List<Long> connIds) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.delSceneConnData(implUser.getId(), connIds);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/scene/{sid}/var")
    @ApiOperation(value = "获取组态变量相关数据")
    @ApiOperationSupport(order = 80)
    public ResponseUtils<PageResult<Variable>> getSceneVarData(
            @ApiParam(value = "组态id", required = true) @PathVariable("sid") Long sid,
            @ApiParam(value = "模式,1:可作为模拟量触发变量的所有变量，2：可作为离散量触发的所有变量")
            @RequestParam(value = "mode", required = false) Integer mode,
            @Valid PageRequest request) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult sceneVarData = sceneService.getSceneVarData(request, implUser.getId(), sid, mode);
        return responseUtils.buildSuccessResponse(sceneVarData);
    }

    @GetMapping("/scene/var/{varId}")
    @ApiOperation(value = "获取组态变量一条数据")
    @ApiOperationSupport(order = 81)
    public ResponseUtils<Variable> getOneSceneVarData(
            @ApiParam(value = "变量id", required = true) @PathVariable("varId") Long varId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        Variable oneSceneVarData = sceneService.getOneSceneVarData(implUser.getId(), varId);
        return responseUtils.buildSuccessResponse(oneSceneVarData);
    }

    @PostMapping("/scene/var")
    @ApiOperation(value = "创建组态变量")
    @ApiOperationSupport(order = 82, includeParameters = {"var.varNo", "var.dataType",
            "var.len", "var.varAddr", "var.sceneId", "var.connectNo", "var.name"})
    public ResponseUtils saveSceneVarData(@Valid @RequestBody Variable var) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.saveSceneVarData(implUser.getId(), var);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/scene/var")
    @ApiOperation(value = "修改组态变量")
    @ApiOperationSupport(order = 84, ignoreParameters = {"var.area", "var.subArea", "var.addOffset", "var.bitOffset",
            "var.cycleTime", "var.sceneId"})
    public ResponseUtils updateSceneVarData(
            @Validated({IdGroup.class, Default.class}) @RequestBody Variable var) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.updateSceneVarData(implUser.getId(), var);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/scene/var")
    @ApiOperation(value = "删除组态变量")
    @ApiOperationSupport(order = 86)
    public ResponseUtils deleteSceneVarData(@RequestBody @ListNotHasNull @Size(min = 1) List<Long> varIds) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.delSceneVarData(implUser.getId(), varIds);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/scene/{sid}/analog/alarm")
    @ApiOperation(value = "获取组态模拟量相关数据")
    @ApiOperationSupport(order = 90)
    public ResponseUtils<PageResult<AnalogAlarm>> getSceneAnalogAlarm(
            @ApiParam(value = "组态id", required = true) @PathVariable("sid") Long sid,
            @Valid PageRequest request) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult sceneAnalogAlarm = sceneService.getSceneAnalogAlarm(request, implUser.getId(), sid);
        return responseUtils.buildSuccessResponse(sceneAnalogAlarm);
    }

    @GetMapping("/scene/analog/{analogId}")
    @ApiOperation(value = "获取组态模拟量一条数据")
    @ApiOperationSupport(order = 91)
    public ResponseUtils<AnalogAlarm> getOneSceneAnalogData(
            @ApiParam(value = "模拟量id", required = true) @PathVariable("analogId") Long analogId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        AnalogAlarm oneSceneAnalogData = sceneService.getOneSceneAnalogData(implUser.getId(), analogId);
        return responseUtils.buildSuccessResponse(oneSceneAnalogData);
    }

    @PostMapping("/scene/analog")
    @ApiOperation(value = "创建组态模拟量")
    @ApiOperationSupport(order = 92, ignoreParameters = {"analog.id"})
    public ResponseUtils saveSceneAnalogData(
            @Valid @RequestBody AnalogAlarm analog) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.saveSceneAnalogData(implUser.getId(), analog);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/scene/analog")
    @ApiOperation(value = "修改组态模拟量")
    @ApiOperationSupport(order = 94, ignoreParameters = {"analog.sceneId"})
    public ResponseUtils updateSceneAnalogData(
            @Validated({IdGroup.class, Default.class}) @RequestBody AnalogAlarm analog) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.updateSceneAnalogData(implUser.getId(), analog);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/scene/analog")
    @ApiOperation(value = "删除组态模拟量")
    @ApiOperationSupport(order = 96)
    public ResponseUtils deleteSceneAnalogData(
            @RequestBody @ListNotHasNull @Size(min = 1) List<Long> analogIds) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.delSceneAnalogData(implUser.getId(), analogIds);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/scene/{sid}/discrete/alarm")
    @ApiOperation(value = "获取组态离散量相关数据")
    @ApiOperationSupport(order = 100)
    public ResponseUtils<PageResult<DiscreteAlarm>> getSceneDiscreteAlarm(
            @ApiParam(value = "组态id", required = true) @PathVariable("sid") Long sid,
            @Valid PageRequest request) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult sceneDiscreteAlarm = sceneService.getSceneDiscreteAlarm(request, implUser.getId(), sid);
        return responseUtils.buildSuccessResponse(sceneDiscreteAlarm);
    }

    @GetMapping("/scene/discrete/{discreteId}")
    @ApiOperation(value = "获取组态离散量一条数据")
    @ApiOperationSupport(order = 101)
    public ResponseUtils<DiscreteAlarm> getOneSceneDiscreteData(
            @ApiParam(value = "离散量id", required = true) @PathVariable("discreteId") Long discreteId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        DiscreteAlarm oneSceneDiscreteData = sceneService.getOneSceneDiscreteData(implUser.getId(), discreteId);
        return responseUtils.buildSuccessResponse(oneSceneDiscreteData);
    }

    @PostMapping("/scene/discrete")
    @ApiOperation(value = "创建组态离散量")
    @ApiOperationSupport(order = 102, ignoreParameters = {"discrete.id"})
    public ResponseUtils saveSceneDiscreteData(
            @Valid @RequestBody DiscreteAlarm discrete) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.saveSceneDiscreteData(implUser.getId(), discrete);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/scene/discrete")
    @ApiOperation(value = "修改组态离散量")
    @ApiOperationSupport(order = 104, ignoreParameters = {"discrete.sceneId"})
    public ResponseUtils updateSceneDiscreteData(
            @Validated({IdGroup.class, Default.class}) @RequestBody DiscreteAlarm discrete) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.updateSceneDiscreteData(implUser.getId(), discrete);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/scene/discrete")
    @ApiOperation(value = "删除组态离散量")
    @ApiOperationSupport(order = 106)
    public ResponseUtils deleteSceneDiscreteData(
            @RequestBody @ListNotHasNull @Size(min = 1) List<Long> discreteIds) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.delSceneDiscreteData(implUser.getId(), discreteIds);
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/scene/data/{sceneId}")
    @ApiOperation(value = "发布组态", notes = "组态发布成功之后才能运行")
    @ApiOperationSupport(order = 110)
    public ResponseUtils publicSceneData(@PathVariable("sceneId") Long sceneId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.publishSceneData(implUser.getId(), sceneId);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/scene/{sceneId}/device")
    @ApiOperation(value = "获取组态中的连接的配置设备", notes = "用于组态连接配置功能，呈现两列，左边列是组态的连接名称，右边列是设备名称，一一对应" +
            "设备id为-1表示没有配置设备")
    @ApiOperationSupport(order = 112)
    public ResponseUtils<List<ConnectDevice>> getSceneDevice(@PathVariable("sceneId") @ApiParam(value = "组态id",
            required = true) Long sceneId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        List<ConnectDevice> sceneDevice = sceneService.getSceneDevice(implUser.getId(), sceneId);
        return responseUtils.buildSuccessResponse(sceneDevice);
    }

    @GetMapping("/scene/{sceneId}/config/device")
    @ApiOperation(value = "获取可用于配置组态连接的设备")
    @ApiOperationSupport(order = 114)
    public ResponseUtils<List<ConnectDevice>> getSceneConfigDevice(@PathVariable("sceneId") @ApiParam(value = "组态id",
            required = true) Long sceneId) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        List<ConnectDevice> sceneConfigDevice = sceneService.getSceneConfigDevice(implUser.getId(), sceneId);
        return responseUtils.buildSuccessResponse(sceneConfigDevice);
    }

    @PutMapping("/scene/device")
    @ApiOperation(value = "给组态配置设备", notes = "一个连接只能配一个设备，一个设备也只能配一个连接，" +
            "设备id值传-1，则为取消连接配置设备")
    @ApiOperationSupport(order = 116)
    public ResponseUtils updateConnDevice(@RequestBody @Valid ConnDevice connDevice) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.updateConnDevice(implUser.getId(), connDevice);
        return responseUtils.buildSuccessResponse();
    }

    @PostMapping("/scene/{sceneId}/run/{command}")
    @ApiOperation(value = "运行组态", notes = "组态发布成功之后才能运行")
    @ApiOperationSupport(order = 120)
    public ResponseUtils runScene(@PathVariable("sceneId") @ApiParam(value = "组态id", required = true) Long sceneId,
                                  @PathVariable("command") @ApiParam(value = "操作指令，0表示停止，1表示运行", required = true)
                                          Boolean command) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        sceneService.runScene(implUser.getId(), sceneId, command);
        return responseUtils.buildSuccessResponse();
    }

    @GetMapping("/scene/connect/driveTypes")
    @ApiOperation(value = "获取所有驱动类型")
    @ApiOperationSupport(order = 130)
    public ResponseUtils<List<DriveType>> getAllDriveType() {
        List<DriveType> allDriveType = sceneService.getAllDriveType();
        return responseUtils.buildSuccessResponse(allDriveType);
    }

    @GetMapping("/scene/var/addr/driveType/{num}")
    @ApiOperation(value = "通过驱动类型编号获取组态变量类型，长度，地址,取值范围", notes = "num=2有值，其他暂时没有")
    @ApiOperationSupport(order = 140)
    public ResponseUtils<List<VarAttribute>> getVarAttrByDriveType(
            @PathVariable("num") @ApiParam(value = "驱动类型编号", required = true) Integer num) {
        List<VarAttribute> varAddrByDriveTypeAndDataType = sceneService.getVarAddrByDriveTypeAndDataType(num);
        return responseUtils.buildSuccessResponse(varAddrByDriveTypeAndDataType);
    }

    @GetMapping("/scene/{sid}/devices")
    @ApiOperation(value = "获取组态配置的设备信息", notes = "地图监控中用于获取组态位置，因为组态会有多个设备，所以取第一个设备的位置")
    @ApiOperationSupport(order = 210)
    public ResponseUtils<List<Device>> getSceneDevices(@PathVariable("sid") Long sid) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        List<Device> sceneDevices = sceneService.getSceneDevices(implUser.getId(), sid);
        return responseUtils.buildSuccessResponse(sceneDevices);
    }

    @GetMapping("/scene/{sid}/var/{varNo}")
    @ApiOperation(value = "根据组态id和变量序号获取组态变量信息")
    @ApiOperationSupport(order = 220)
    public ResponseUtils<Variable> getSceneVarInfo(@PathVariable("sid") @ApiParam(value = "组态id", required = true) Long sid,
                                                   @PathVariable("varNo") @ApiParam(value = "变量序号", required = true) Integer varNo) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        Variable sceneVarInfo = sceneService.getSceneVarInfo(implUser.getId(), sid, varNo);
        return responseUtils.buildSuccessResponse(sceneVarInfo);
    }
}
